#include "lanefunction.h"
#include <stdlib.h>
/*!
 \brief
    将十六进制的char型字符转换成对应的int值
 \param c
 \return int
*/
inline int char2Hex(char c)
{
    if ('0'<=c && c<='9') return c-'0';
    if ('a'<=c && c<='f') return c-'a'+10;
    if ('A'<=c && c<='F') return c-'A'+10;
    return -1;
}

/*!
 \brief
 \param b
 \return char
*/
inline char hex2Char(quint8 b)
{
    if (b<10) return b+'0';
    if (b<16) return b-10+'a';
    return '0';
}
quint8 qHiByte(quint16 wValue)
{
    return (quint8)((wValue >> 8) & 0xFF );
}


quint8 qLoByte(quint16 wValue)
{
    return (quint8)(wValue & 0xFF);
}
void getCheckCode(const char *buffer, const int length, char *verifyCode)
{
    if(buffer == NULL || verifyCode == NULL)
    {
        return;
    }
    int rowIndex = length / 8;
    int leftIndex = length % 8;
    memset(verifyCode, 0, 8);
    for(int i = 0; i < rowIndex; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            verifyCode[j] = verifyCode[j] ^ buffer[i * 8 + j];
        }
    }

    for(int j = 0; j < leftIndex; j++)
    {
        verifyCode[j] = verifyCode[j] ^ buffer[rowIndex * 8 + j];
    }
}


bool systime2bcd(char *bcd, int len, const QDateTime &sysTime)
{
    if ((len<2) || (len>7))
    {
        return false;
    }
    char buf[64];

    strcpy(buf, sysTime.toString("yyyyMMddhhmmss").toLocal8Bit().data());
    hex2Raw((quint8 *)bcd, buf, min(len*2, 14));
    return true;
}


bool hex2Raw(quint8 *pData, const char *szMsg, int msgLen)
{
    int hi, lo;
    for (int i=0;i<msgLen/2;i++)
    {
        hi=char2Hex(szMsg[i*2]);
        lo=char2Hex(szMsg[i*2+1]);
        if (hi<0 || lo<0) return false;
        pData[i]=(hi<<4)+lo;
    }
    return true;
}


void dateTimeToChar14(const QDateTime &dateTime, char *timeChar14)
{
    if(dateTime.isValid())
    {
        memcpy(timeChar14, dateTime.toString("yyyyMMddhhmmss").toLocal8Bit().data(), 14);
    }
}


int ulong2StrBuf(char *strBuff, size_t bufferLen, unsigned long ulong)
{
    if(bufferLen < 1)
        return -1;
    for (int i = (int)bufferLen - 1; i >= 0;i --)
    {
        strBuff[i] = char(ulong % 10 + '0');
        ulong /= 10;
    }
    return ulong;
}
quint32 bcd2dword(const char *bcd, int len)
{
    quint32 u=0;
    for (int i=0; i<len; i++)
    {
        if (0xFF==(quint8)(bcd[i]))
        {
            break;
        }
        if (0x0F==(bcd[i]&0x0F))
        {
            u=bcd2byte(bcd[i])+u*10;
            break;
        }
        u=bcd2byte(bcd[i])+u*100;
    }
    return u;
}

bool bcd2date(QDate *pDate, const quint8 *bcd)
{
    pDate->setDate(bcd2byte(bcd[0])*100+bcd2byte(bcd[1]),
            bcd2byte(bcd[2]), bcd2byte(bcd[3]));
    return pDate->isValid();
}

bool bcd2systime(QDateTime *pTime, const char *bcd, int bcdLen)
{
    pTime->setDate(QDate(
                       (quint16)bcd2dword(bcd, 2),
                       bcd2byte(bcd[2]),
                       bcd2byte(bcd[3])));
    if (bcdLen>=7)
    {
        pTime->setTime(QTime(
                           bcd2byte(bcd[4]),
                           bcd2byte(bcd[5]),
                           bcd2byte(bcd[6])));
    }

    return pTime->isValid();
}
quint8 bcd2byte( quint8 bcd )
{
    quint8 Hi= (bcd >> 4) &0x0F;
    quint8 Lo = (bcd) &0x0F;
    if(0x0F == Lo)
        return Hi;
    else
        return Hi*10+ Lo;
}


void raw2HexStr(char *hexStr, const quint8 *rawData, int rawLen)
{
   quint8 b;
   for(int i = 0; i < rawLen; i++)
   {
       b = rawData[i];
       hexStr[2*i]=hex2Char(b>>4);
       hexStr[2*i+1]=hex2Char(b & 0x0f);
   }
   hexStr[2 * rawLen] = 0;
}


int charToInt(const char *buff, int len)
{
    if ((NULL==buff) || (len<=0))
    {
        return 0;
    }
    char tmpBuff[11];
    memset(tmpBuff, 0, sizeof(tmpBuff));
    memcpy(tmpBuff, buff, qMin(len, int(sizeof(tmpBuff)-1)));
    return atoi(tmpBuff);
}
